import unittest
from cvirt.cnode.libvirtXmlObject.virtualController import *

class TestController(unittest.TestCase):
    def test_controller(self):
        controller = virtualController(parse_xml='''<controller type='usb' index='0' model='ich9-ehci1'>
	      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x7'/>
	      <master startport='0'/>
	    </controller>''')

        self.assertEqual(controller.type,'usb')
        self.assertEqual(controller.index,'0')
        self.assertEqual(controller.model,'ich9-ehci1')
        self.assertEqual(controller.address.type,'pci')
        self.assertEqual(controller.address.domain,'0x0000')
        self.assertEqual(controller.address.bus,'0x00')
        self.assertEqual(controller.address.slot,'0x08')
        self.assertEqual(controller.address.function,'0x7')

        self.assertEqual(controller.master.startport,'0')

    def test_redirdev(self):
        dev = virtualRedirdev(parse_xml='''<redirdev bus='usb' type='spicevmc'>
	      <address type='usb' bus='0' port='3'/>
	    </redirdev>''')

        self.assertEqual(dev.bus,'usb')
        self.assertEqual(dev.type,'spicevmc')
        self.assertEqual(dev.address.type,'usb')
        self.assertEqual(dev.address.port,'3')


    def test_generate_spice_usbredir(self):
        '''
        	    <controller type='usb' index='0' model='ich9-ehci1'>
	      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x7'/>
	    </controller>
	    <controller type='usb' index='0' model='ich9-uhci1'>
	      <master startport='0'/>
	      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0' multifunction='on'/>
	    </controller>
	    <redirdev bus='usb' type='spicevmc'>
	      <address type='usb' bus='0' port='3'/>
	    </redirdev>
	    <redirdev bus='usb' type='spicevmc'>
	      <address type='usb' bus='0' port='4'/>
	    </redirdev>
        @return:
        '''



        c1 = virtualController()
        c1.type = 'usb'
        c1.index = '0'
        c1.model = 'ich9-ehci1'

        add1 = virtualDeviceAddress()
        add1.type = 'pci'
        add1.domain = '0x0000'
        add1.bus = '0x00'
        add1.slot = '0x08'
        add1.function = '0x7'

        c1.address = add1


        c2 = virtualController()
        c2.type = 'usb'
        c2.index = '0'
        c2.model = 'ich9-ehci1'

        master = virtualDeviceMaster()
        master.startport = '0'

        add2 = virtualDeviceAddress()
        add2.type = 'pci'
        add2.domain = '0x0000'
        add2.bus = '0x00'
        add2.slot = '0x08'
        add2.function = '0x0'
        add2.multifunction = 'on'

        c2.address = add2
        c2.master = master



        red1 = virtualRedirdev()
        red1.bus = 'usb'
        red1.type = 'spicevmc'

        redaddr1 = virtualDeviceAddress()
        redaddr1.type = 'usb'
        redaddr1.bus = '0'
        redaddr1.port = '3'

        red1.address = redaddr1

        red2 = virtualRedirdev()
        red2.bus = 'usb'
        red2.type = 'spicevmc'

        redaddr2 = virtualDeviceAddress()
        redaddr2.type = 'usb'
        redaddr2.bus = '0'
        redaddr2.port = '4'

        red2.address = redaddr2



        print c1.to_xml()
        print c2.to_xml()
        print red1.to_xml()
        print red2.to_xml()
